#include "../include/unrealmapdraw.h"

UnrealMapDraw::UnrealMapDraw(UnrealMap* map):RouteMapDraw(map)
{
    //ctor
}

UnrealMapDraw::~UnrealMapDraw()
{
    //dtor
}


void UnrealMapDraw::drawPath(Node* node, Path* path, Cairo::RefPtr<Cairo::Context> cr){
        double sat = ((UnrealPlace*) node->place)->z - ((UnrealMap*) map)->zMin;
        sat /= ((double) ((UnrealMap*) map)->zMax) - ((double) ((UnrealMap*) map)->zMin);

        cr->save();
        cr->set_source_rgba(1, 1, 1, 0.3+(0.7*sat));

        cr->set_line_width(
                (((UnrealPath*)path)->collisionR^2)
                *16
        );
        cr->move_to(node->place->x, node->place->y);
        cr->line_to(
                    path->to->place->x,
                    path->to->place->y
                    );
        cr->stroke();
        cr->restore();
}
void UnrealMapDraw::drawNode(Node* node, Cairo::RefPtr<Cairo::Context> cr){
        double sat = ((UnrealPlace*) node->place)->z - ((UnrealMap*) map)->zMin;
        sat /= ((double) ((UnrealMap*) map)->zMax) - ((double) ((UnrealMap*) map)->zMin);

        color3 c = nodeColors[((UnrealNodeId*) node->id)->category % nodeColors.size()];

        cr->set_source_rgba(c.r, c.g, c.b , 0.3+(0.7*sat));

//        cr->set_source_rgba(sat, 0, 1-sat, 0.5);
//        cr->set_line_width(900*sat);

        cr->arc(node->place->x, node->place->y, 3000, 0.0, 2.0 * M_PI); // full circle
//        cr->fill_preserve();
        cr->fill();
 //       cr->set_source_rgba(0.2, 0.2, 1, 1);
  //      cr->stroke();
}

void UnrealMapDraw::addBotToDraw(UnrealNativeBot* bot){
	drawBots.push_back(new UnrealNativeBotDraw(bot));
    addDrawable(drawBots.back());
}


void UnrealNativeBotDraw::draw(Cairo::RefPtr<Cairo::Context> cr)
{
	UnrealNativeBot* b = (UnrealNativeBot*) subject;
	if (!subject->map ||
		subject->place->x < subject->map->left  ||
	    subject->place->x > subject->map->right || 
		subject->place->y < subject->map->top   ||
		subject->place->y > subject->map->bottom)
		{
			cout << "wrong coordinates: (" << subject->place->x << "," << subject->place->y << ")\n"
			     << "for bot: " << b->id << endl;
			return;
		}
		
    if(b->team)
        cr->set_source_rgba(0, 0, 1, 0.5);
    else
        cr->set_source_rgba(1, 0, 0, 0.5);

    cr->arc(subject->place->x, subject->place->y, 10000, 0.0, 2.0 * M_PI); // full circle
    cr->fill_preserve();
    cr->set_line_width(5000);
    if(b->team)
        cr->set_source_rgba(0, 0, 1, 1);
    else
        cr->set_source_rgba(1, 0, 0, 1);
    cr->stroke();
}


void UnrealMapDraw::afterDraw(Cairo::RefPtr<Cairo::Context> cr){
/*    for(unsigned i=0; i<drawBots.size(); i++){
        cr->save();
        cr->move_to(drawBots[i]->place.x, drawBots[i]->place.y);
        drawBot(drawBots[i], &drawBots[i]->place, cr);
        cr->restore();
    }*/
}
